package com.xmcowell.sportpacemaster.utils;

import android.content.Context;
import android.util.Log;

import com.xmcowell.sportpacemaster.SerialPortUtil;
import com.xmcowell.sportpacemaster.config.Contents;

/**
 * Created by fanyingmao on 16/12/14.
 */

public class SportSerialPortUtil {
    public static byte head;

    private static boolean unit=false;
    public static void setSD(Context mContext,int d, int s) {

        if (unit){
            head = 0x40;
        } else{
            head = 0x60;
        }
        getComment(head + d, s, false);
    }

    public static void  setUnit(boolean bunit){
        unit=bunit;
    }

    public static void setStatus(int status) {
        getComment(0x80, status, true);
    }

    /**
     * @return
     */
    public static void getComment(int cc, int dd, boolean isStatus) {
        byte[] data = {(byte) 0xfb, 0x00, 0x00, 0x00, (byte) 0xfc};
        data[1] = (byte) cc;
        data[2] = (byte) dd;
        data[3] = (byte) (data[1] + data[2]);
        //if(!isStatus) {
            logDate(data);
        //}
        //Log.i("data", "===========================star=================== ");
        SerialPortUtil.getInstance().sendBuffer(data);
    }

    public static void  sendSlopeSpeed(Context mContext, int slope, int speed){
        if(Contents.isBicycle){
            setSD(mContext, slope-3, speed);
        }else {
            setSD(mContext, slope, speed);
        }
    }

    public  static void sendData(int data1, int data2){
        getComment(data1, data2, false);
    }

    public static void logDate(byte[] data) {
        final StringBuilder stringBuilder = new StringBuilder(data.length);
        for (byte byteChar : data)
            stringBuilder.append(String.format("%02X ", byteChar));
//        Log.i("data", "send data = " + stringBuilder.toString());
    }


}
